import { mapValues, trim } from 'lodash'
import { filterOption } from '../../utils/common'
import SjhtSearchSubmit from '../../components/search-submit'
export default {
  components: { [SjhtSearchSubmit.name]: SjhtSearchSubmit },
  data() {
    return {
      filterOption, //过滤选项
      searchForm: this.$form.createForm(this, { name: 'SjhtSearchForm' })
    }
  },
  methods: {
    //查询表单submit事件
    searchSubmit(e) {
      e.preventDefault()
      this.searchForm.validateFields((err, values) => {
        if (err) return false
        this.onSearch(values)
      })
    },
    //查询表单函数
    onSearch(values) {
      const { pagination } = this
      pagination.current = 1
      const params = mapValues(values, v => (/[\S]+/.test(trim(v)) ? trim(v) : undefined))
      Object.assign(params, {
        current: undefined,
        pageSize: pagination.pageSize > 10 ? pagination.pageSize : undefined
      })
      Object.assign(this.loadParams, params)
      this.loadData()
    },
    //查询表单重置事件
    onReset() {
      this.searchForm.resetFields()
      this.pagination.current = 1
      this.pagination.pageSize = 10
      this.loadParams = {}
      this.loadData()
    }
  }
}
